home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / dist / gdb / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-10  |  58.5 KB  |  2,318 lines

  1. /* Top level `main' program for GDB, the GNU debugger.
  2.    Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992
  3.    Free Software Foundation, Inc.
  4.  
  5. This file is part of GDB.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include "defs.h"
  22. #include "gdbcmd.h"
  23. #include "call-cmds.h"
  24. #include "symtab.h"
  25. #include "inferior.h"
  26. #include "signals.h"
  27. #include "target.h"
  28. #include "breakpoint.h"
  29. #include "gdbtypes.h"
  30. #include "expression.h"
  31. #include "language.h"
  32.  
  33. #include "getopt.h"
  34.  
  35. /* readline include files */
  36. #include "readline.h"
  37. #include "history.h"
  38.  
  39. /* readline defines this.  */
  40. #undef savestring
  41.  
  42. #ifdef USG
  43. #include <sys/types.h>
  44. #include <unistd.h>
  45. #endif
  46.  
  47. #include <string.h>
  48. #ifndef    NO_SYS_FILE
  49. #include <sys/file.h>
  50. #endif
  51. #include <setjmp.h>
  52. #include <sys/param.h>
  53. #include <sys/stat.h>
  54. #include <ctype.h>
  55.  
  56. #ifdef SET_STACK_LIMIT_HUGE
  57. #include <sys/time.h>
  58. #include <sys/resource.h>
  59.  
  60. int original_stack_limit;
  61. #endif
  62.  
  63. /* Prototypes for local functions */
  64.  
  65. static char *
  66. symbol_completion_function PARAMS ((char *, int));
  67.  
  68. static void
  69. command_loop PARAMS ((void));
  70.  
  71. static void
  72. command_loop_marker PARAMS ((int));
  73.  
  74. static void
  75. print_gdb_version PARAMS ((void));
  76.  
  77. static void
  78. quit_command PARAMS ((char *, int));
  79.  
  80. static void
  81. initialize_main PARAMS ((void));
  82.  
  83. static void
  84. initialize_history PARAMS ((void));
  85.  
  86. static void
  87. initialize_cmd_lists PARAMS ((void));
  88.  
  89. static void
  90. float_handler PARAMS ((int));
  91.  
  92. static void
  93. source_command PARAMS ((char *, int));
  94.  
  95. static void
  96. cd_command PARAMS ((char *, int));
  97.  
  98. static void
  99. print_gnu_advertisement PARAMS ((void));
  100.  
  101. static void
  102. init_signals PARAMS ((void));
  103.  
  104. static void
  105. read_command_file PARAMS ((FILE *));
  106.  
  107. static void 
  108. set_verbose PARAMS ((char *, int, struct cmd_list_element *));
  109.  
  110. static void
  111. show_history PARAMS ((char *, int));
  112.  
  113. static void
  114. set_history PARAMS ((char *, int));
  115.  
  116. static void
  117. set_history_size_command PARAMS ((char *, int, struct cmd_list_element *));
  118.  
  119. static void
  120. show_commands PARAMS ((char *, int));
  121.  
  122. static void
  123. dump_me_command PARAMS ((char *, int));
  124.  
  125. static void
  126. echo_command PARAMS ((char *, int));
  127.  
  128. static void
  129. pwd_command PARAMS ((char *, int));
  130.  
  131. static void
  132. show_version PARAMS ((char *, int));
  133.  
  134. static void
  135. document_command PARAMS ((char *, int));
  136.  
  137. static void
  138. define_command PARAMS ((char *, int));
  139.  
  140. static void
  141. validate_comname PARAMS ((char *));
  142.  
  143. static void
  144. help_command PARAMS ((char *, int));
  145.  
  146. static void
  147. show_command PARAMS ((char *, int));
  148.  
  149. static void
  150. info_command PARAMS ((char *, int));
  151.  
  152. static void
  153. do_nothing PARAMS ((int));
  154.  
  155. static void
  156. disconnect PARAMS ((int));
  157.  
  158. static void
  159. source_cleanup PARAMS ((FILE *));
  160.  
  161. /* If this definition isn't overridden by the header files, assume
  162.    that isatty and fileno exist on this system.  */
  163. #ifndef ISATTY
  164. #define ISATTY(FP)    (isatty (fileno (FP)))
  165. #endif
  166.  
  167. /* Initialization file name for gdb.  This is overridden in some configs.  */
  168.  
  169. #ifndef    GDBINIT_FILENAME
  170. #define    GDBINIT_FILENAME    ".gdbinit"
  171. #endif
  172. char gdbinit[] = GDBINIT_FILENAME;
  173.  
  174. #define    ALL_CLEANUPS    ((struct cleanup *)0)
  175.  
  176. /* Version number of GDB, as a string.  */
  177.  
  178. extern char *version;
  179.  
  180. /* Message to be printed before the error message, when an error occurs.  */
  181.  
  182. extern char *error_pre_print;
  183.  
  184. /* Message to be printed before the warning message, when a warning occurs.  */
  185.  
  186. extern char *warning_pre_print;
  187.  
  188. extern char lang_frame_mismatch_warn[];        /* language.c */
  189.  
  190. /* Whether GDB's stdin is on a terminal.  */
  191.  
  192. extern int gdb_has_a_terminal;            /* inflow.c */
  193.  
  194. /* Flag for whether we want all the "from_tty" gubbish printed.  */
  195.  
  196. int caution = 1;            /* Default is yes, sigh. */
  197.  
  198. /*
  199.  * Define all cmd_list_element's
  200.  */
  201.  
  202. /* Chain containing all defined commands.  */
  203.  
  204. struct cmd_list_element *cmdlist;
  205.  
  206. /* Chain containing all defined info subcommands.  */
  207.  
  208. struct cmd_list_element *infolist;
  209.  
  210. /* Chain containing all defined enable subcommands. */
  211.  
  212. struct cmd_list_element *enablelist;
  213.  
  214. /* Chain containing all defined disable subcommands. */
  215.  
  216. struct cmd_list_element *disablelist;
  217.  
  218. /* Chain containing all defined delete subcommands. */
  219.  
  220. struct cmd_list_element *deletelist;
  221.  
  222. /* Chain containing all defined "enable breakpoint" subcommands. */
  223.  
  224. struct cmd_list_element *enablebreaklist;
  225.  
  226. /* Chain containing all defined set subcommands */
  227.  
  228. struct cmd_list_element *setlist;
  229.  
  230. /* Chain containing all defined show subcommands.  */
  231. struct cmd_list_element *showlist;
  232.  
  233. /* Chain containing all defined \"set history\".  */
  234.  
  235. struct cmd_list_element *sethistlist;
  236.  
  237. /* Chain containing all defined \"show history\".  */
  238. struct cmd_list_element *showhistlist;
  239.  
  240. /* Chain containing all defined \"unset history\".  */
  241.  
  242. struct cmd_list_element *unsethistlist;
  243.  
  244. /* stdio stream that command input is being read from.  */
  245.  
  246. FILE *instream;
  247.  
  248. /* Current working directory.  */
  249.  
  250. char *current_directory;
  251.  
  252. /* The directory name is actually stored here (usually).  */
  253. static char dirbuf[1024];
  254.  
  255. /* Function to call before reading a command, if nonzero.
  256.    The function receives two args: an input stream,
  257.    and a prompt string.  */
  258.  
  259. void (*window_hook) PARAMS ((FILE *, char *));
  260.  
  261. extern int frame_file_full_name;
  262. extern int mapped_symbol_files;
  263. extern int readnow_symbol_files;
  264.  
  265. int epoch_interface;
  266. int xgdb_verbose;
  267.  
  268. /* gdb prints this when reading a command interactively */
  269. static char *prompt;
  270.  
  271. /* Buffer used for reading command lines, and the size
  272.    allocated for it so far.  */
  273.  
  274. char *line;
  275. int linesize = 100;
  276.  
  277. /* Baud rate specified for talking to serial target systems.  Default
  278.    is left as a zero pointer, so targets can choose their own defaults.  */
  279.  
  280. char *baud_rate;
  281.  
  282. /* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT.  */
  283.  
  284. #ifndef STOP_SIGNAL
  285. #ifdef SIGTSTP
  286. #define STOP_SIGNAL SIGTSTP
  287. static void stop_sig PARAMS ((int));
  288. #endif
  289. #endif
  290.  
  291. /* Some System V have job control but not sigsetmask(). */
  292. #if !defined (HAVE_SIGSETMASK)
  293. #define HAVE_SIGSETMASK !defined (USG)
  294. #endif
  295.  
  296. #if 0 == (HAVE_SIGSETMASK)
  297. #define sigsetmask(n)
  298. #endif
  299.  
  300. /* This is how `error' returns to command level.  */
  301.  
  302. jmp_buf to_top_level;
  303.  
  304. NORETURN void
  305. return_to_top_level ()
  306. {
  307.   quit_flag = 0;
  308.   immediate_quit = 0;
  309.   bpstat_clear_actions(stop_bpstat);    /* Clear queued breakpoint commands */
  310.   disable_current_display ();
  311.   do_cleanups (ALL_CLEANUPS);
  312.   (NORETURN void) longjmp (to_top_level, 1);
  313. }
  314.  
  315. /* Call FUNC with arg ARGS, catching any errors.
  316.    If there is no error, return the value returned by FUNC.
  317.    If there is an error, print ERRSTRING, print the specific error message,
  318.                  then return zero.  */
  319.  
  320. int
  321. catch_errors (func, args, errstring)
  322.      int (*func) PARAMS ((char *));
  323.      char *args;
  324.      char *errstring;
  325. {
  326.   jmp_buf saved;
  327.   int val;
  328.   struct cleanup *saved_cleanup_chain;
  329.   char *saved_error_pre_print;
  330.  
  331.   saved_cleanup_chain = save_cleanups ();
  332.   saved_error_pre_print = error_pre_print;
  333.  
  334.   memcpy ((char *)saved, (char *)to_top_level, sizeof (jmp_buf));
  335.   error_pre_print = errstring;
  336.  
  337.   if (setjmp (to_top_level) == 0)
  338.     val = (*func) (args);
  339.   else
  340.     val = 0;
  341.  
  342.   restore_cleanups (saved_cleanup_chain);
  343.  
  344.   error_pre_print = saved_error_pre_print;
  345.   memcpy ((char *)to_top_level, (char *)saved, sizeof (jmp_buf));
  346.   return val;
  347. }
  348.  
  349. /* Handler for SIGHUP.  */
  350.  
  351. static void
  352. disconnect (signo)
  353. int signo;
  354. {
  355.   kill_inferior_fast ();
  356.   signal (signo, SIG_DFL);
  357.   kill (getpid (), SIGHUP);
  358. }
  359.  
  360. /* Clean up on error during a "source" command (or execution of a
  361.    user-defined command).  */
  362.  
  363. static void
  364. source_cleanup (stream)
  365.      FILE *stream;
  366. {
  367.   /* Restore the previous input stream.  */
  368.   instream = stream;
  369. }
  370.  
  371. /* Read commands from STREAM.  */
  372. static void
  373. read_command_file (stream)
  374.      FILE *stream;
  375. {
  376.   struct cleanup *cleanups;
  377.  
  378.   cleanups = make_cleanup (source_cleanup, instream);
  379.   instream = stream;
  380.   command_loop ();
  381.   do_cleanups (cleanups);
  382. }
  383.  
  384.  
  385. int
  386. main (argc, argv)
  387.      int argc;
  388.      char **argv;
  389. {
  390.   int count;
  391.   static int inhibit_gdbinit = 0;
  392.   static int quiet = 0;
  393.   static int batch = 0;
  394.  
  395.   /* Pointers to various arguments from command line.  */
  396.   char *symarg = NULL;
  397.   char *execarg = NULL;
  398.   char *corearg = NULL;
  399.   char *cdarg = NULL;
  400.   char *ttyarg = NULL;
  401.  
  402.   /* Pointers to all arguments of +command option.  */
  403.   char **cmdarg;
  404.   /* Allocated size of cmdarg.  */
  405.   int cmdsize;
  406.   /* Number of elements of cmdarg used.  */
  407.   int ncmd;
  408.  
  409.   /* Indices of all arguments of +directory option.  */
  410.   char **dirarg;
  411.   /* Allocated size.  */
  412.   int dirsize;
  413.   /* Number of elements used.  */
  414.   int ndir;
  415.   
  416.   register int i;
  417.  
  418.   /* This needs to happen before the first use of malloc.  */
  419.   init_malloc ((PTR) NULL);
  420.  
  421. #if defined (ALIGN_STACK_ON_STARTUP)
  422.   i = (int) &count & 0x3;
  423.   if (i != 0)
  424.     alloca (4 - i);
  425. #endif
  426.  
  427.   /* If error() is called from initialization code, just exit */
  428.   if (setjmp (to_top_level)) {
  429.     exit(1);
  430.   }
  431.  
  432.   cmdsize = 1;
  433.   cmdarg = (char **) xmalloc (cmdsize * sizeof (*cmdarg));
  434.   ncmd = 0;
  435.   dirsize = 1;
  436.   dirarg = (char **) xmalloc (dirsize * sizeof (*dirarg));
  437.   ndir = 0;
  438.  
  439.   quit_flag = 0;
  440.   line = (char *) xmalloc (linesize);
  441.   line[0] = '\0';        /* Terminate saved (now empty) cmd line */
  442.   instream = stdin;
  443.  
  444.   getcwd (dirbuf, sizeof (dirbuf));
  445.   current_directory = dirbuf;
  446.  
  447. #ifdef SET_STACK_LIMIT_HUGE
  448.   {
  449.     struct rlimit rlim;
  450.  
  451.     /* Set the stack limit huge so that alloca (particularly stringtab
  452.      * in dbxread.c) does not fail. */
  453.     getrlimit (RLIMIT_STACK, &rlim);
  454.     original_stack_limit = rlim.rlim_cur;
  455.     rlim.rlim_cur = rlim.rlim_max;
  456.     setrlimit (RLIMIT_STACK, &rlim);
  457.   }
  458. #endif /* SET_STACK_LIMIT_HUGE */
  459.  
  460.   /* Parse arguments and options.  */
  461.   {
  462.     int c;
  463.     static int print_help;
  464.     /* When var field is 0, use flag field to record the equivalent
  465.        short option (or arbitrary numbers starting at 10 for those
  466.        with no equivalent).  */
  467.     static struct option long_options[] =
  468.       {
  469.     {"readnow", no_argument, &readnow_symbol_files, 1},
  470.     {"r", no_argument, &readnow_symbol_files, 1},
  471.     {"mapped", no_argument, &mapped_symbol_files, 1},
  472.     {"m", no_argument, &mapped_symbol_files, 1},
  473.     {"quiet", no_argument, &quiet, 1},
  474.     {"q", no_argument, &quiet, 1},
  475.     {"nx", no_argument, &inhibit_gdbinit, 1},
  476.     {"n", no_argument, &inhibit_gdbinit, 1},
  477.     {"batch", no_argument, &batch, 1},
  478.     {"epoch", no_argument, &epoch_interface, 1},
  479.     {"fullname", no_argument, &frame_file_full_name, 1},
  480.     {"f", no_argument, &frame_file_full_name, 1},
  481.     {"help", no_argument, &print_help, 1},
  482.     {"se", required_argument, 0, 10},
  483.     {"symbols", required_argument, 0, 's'},
  484.     {"s", required_argument, 0, 's'},
  485.     {"exec", required_argument, 0, 'e'},
  486.     {"e", required_argument, 0, 'e'},
  487.     {"core", required_argument, 0, 'c'},
  488.     {"c", required_argument, 0, 'c'},
  489.     {"command", required_argument, 0, 'x'},
  490.     {"x", required_argument, 0, 'x'},
  491.     {"directory", required_argument, 0, 'd'},
  492.     {"cd", required_argument, 0, 11},
  493.     {"tty", required_argument, 0, 't'},
  494.     {"baud", required_argument, 0, 'b'},
  495.     {"b", required_argument, 0, 'b'},
  496. /* Allow machine descriptions to add more options... */
  497. #ifdef ADDITIONAL_OPTIONS
  498.     ADDITIONAL_OPTIONS
  499. #endif
  500.     {0, no_argument, 0, 0},
  501.       };
  502.  
  503.     while (1)
  504.       {
  505.     int option_index;
  506.  
  507.     c = getopt_long_only (argc, argv, "",
  508.                   long_options, &option_index);
  509.     if (c == EOF)
  510.       break;
  511.  
  512.     /* Long option that takes an argument.  */
  513.     if (c == 0 && long_options[option_index].flag == 0)
  514.       c = long_options[option_index].val;
  515.  
  516.     switch (c)
  517.       {
  518.       case 0:
  519.         /* Long option that just sets a flag.  */
  520.         break;
  521.       case 10:
  522.         symarg = optarg;
  523.         execarg = optarg;
  524.         break;
  525.       case 11:
  526.         cdarg = optarg;
  527.         break;
  528.       case 's':
  529.         symarg = optarg;
  530.         break;
  531.       case 'e':
  532.         execarg = optarg;
  533.         break;
  534.       case 'c':
  535.         corearg = optarg;
  536.         break;
  537.       case 'x':
  538.         cmdarg[ncmd++] = optarg;
  539.         if (ncmd >= cmdsize)
  540.           {
  541.         cmdsize *= 2;
  542.         cmdarg = (char **) xrealloc ((char *)cmdarg,
  543.                          cmdsize * sizeof (*cmdarg));
  544.           }
  545.         break;
  546.       case 'd':
  547.         dirarg[ndir++] = optarg;
  548.         if (ndir >= dirsize)
  549.           {
  550.         dirsize *= 2;
  551.         dirarg = (char **) xrealloc ((char *)dirarg,
  552.                          dirsize * sizeof (*dirarg));
  553.           }
  554.         break;
  555.       case 't':
  556.         ttyarg = optarg;
  557.         break;
  558.       case 'q':
  559.         quiet = 1;
  560.         break;
  561.       case 'b':
  562.         baud_rate = optarg;
  563.         break;
  564. #ifdef ADDITIONAL_OPTION_CASES
  565.       ADDITIONAL_OPTION_CASES
  566. #endif
  567.       case '?':
  568.         fprintf (stderr,
  569.              "Use `%s +help' for a complete list of options.\n",
  570.              argv[0]);
  571.         exit (1);
  572.       }
  573.  
  574.       }
  575.     if (print_help)
  576.       {
  577.     fputs ("\
  578. This is GDB, the GNU debugger.  Use the command\n\
  579.     gdb [options] [executable [core-file]]\n\
  580. to enter the debugger.\n\
  581. \n\
  582. Options available are:\n\
  583.   -help             Print this message.\n\
  584.   -quiet            Do not print version number on startup.\n\
  585.   -fullname         Output information used by emacs-GDB interface.\n\
  586.   -epoch            Output information used by epoch emacs-GDB interface.\n\
  587.   -batch            Exit after processing options.\n\
  588.   -nx               Do not read .gdbinit file.\n\
  589.   -tty=TTY          Use TTY for input/output by the program being debugged.\n\
  590.   -cd=DIR           Change current directory to DIR.\n\
  591.   -directory=DIR    Search for source files in DIR.\n\
  592.   -command=FILE     Execute GDB commands from FILE.\n\
  593.   -symbols=SYMFILE  Read symbols from SYMFILE.\n\
  594.   -exec=EXECFILE    Use EXECFILE as the executable.\n\
  595.   -se=FILE          Use FILE as symbol file and executable file.\n\
  596.   -core=COREFILE    Analyze the core dump COREFILE.\n\
  597.   -b BAUDRATE       Set serial port baud rate used for remote debugging.\n\
  598.   -mapped           Use mapped symbol files if supported on this system.\n\
  599.   -readnow          Fully read symbol files on first access.\n\
  600. ", stderr);
  601. #ifdef ADDITIONAL_OPTION_HELP
  602.     fputs (ADDITIONAL_OPTION_HELP, stderr);
  603. #endif
  604.     fputs ("\n\
  605. For more information, type \"help\" from within GDB, or consult the\n\
  606. GDB manual (available as on-line info or a printed manual).\n", stderr);
  607.     /* Exiting after printing this message seems like
  608.        the most useful thing to do.  */
  609.     exit (0);
  610.       }
  611.     
  612.     /* OK, that's all the options.  The other arguments are filenames.  */
  613.     count = 0;
  614.     for (; optind < argc; optind++)
  615.       switch (++count)
  616.     {
  617.     case 1:
  618.       symarg = argv[optind];
  619.       execarg = argv[optind];
  620.       break;
  621.     case 2:
  622.       corearg = argv[optind];
  623.       break;
  624.     case 3:
  625.       fprintf (stderr,
  626.            "Excess command line arguments ignored. (%s%s)\n",
  627.            argv[optind], (optind == argc - 1) ? "" : " ...");
  628.       break;
  629.     }
  630.     if (batch)
  631.       quiet = 1;
  632.   }
  633.  
  634.   /* Run the init function of each source file */
  635.  
  636.   initialize_cmd_lists ();    /* This needs to be done first */
  637.   initialize_all_files ();
  638.   initialize_main ();        /* But that omits this file!  Do it now */
  639.   init_signals ();
  640.  
  641.   if (!quiet)
  642.     {
  643.       /* Print all the junk at the top, with trailing "..." if we are about
  644.      to read a symbol file (possibly slowly).  */
  645.       print_gnu_advertisement ();
  646.       print_gdb_version ();
  647.       if (symarg)
  648.     printf_filtered ("..");
  649.       wrap_here("");
  650.       fflush (stdout);        /* Force to screen during slow operations */
  651.     }
  652.  
  653.   error_pre_print = "\n\n";
  654.   warning_pre_print = "\n\nwarning: ";
  655.  
  656.   /* Now perform all the actions indicated by the arguments.  */
  657.   if (cdarg != NULL)
  658.     {
  659.       if (!setjmp (to_top_level))
  660.     {
  661.       cd_command (cdarg, 0);
  662.       init_source_path ();
  663.     }
  664.     }
  665.   do_cleanups (ALL_CLEANUPS);
  666.  
  667.   for (i = 0; i < ndir; i++)
  668.     if (!setjmp (to_top_level))
  669.       directory_command (dirarg[i], 0);
  670.   free ((PTR)dirarg);
  671.   do_cleanups (ALL_CLEANUPS);
  672.  
  673.   if (execarg != NULL
  674.       && symarg != NULL
  675.       && strcmp (execarg, symarg) == 0)
  676.     {
  677.       /* The exec file and the symbol-file are the same.  If we can't open
  678.      it, better only print one error message.  */
  679.       if (!setjmp (to_top_level))
  680.     {
  681.       exec_file_command (execarg, !batch);
  682.       symbol_file_command (symarg, 0);
  683.     }
  684.     }
  685.   else
  686.     {
  687.       if (execarg != NULL)
  688.     if (!setjmp (to_top_level))
  689.       exec_file_command (execarg, !batch);
  690.       if (symarg != NULL)
  691.     if (!setjmp (to_top_level))
  692.       symbol_file_command (symarg, 0);
  693.     }
  694.   do_cleanups (ALL_CLEANUPS);
  695.  
  696.   /* After the symbol file has been read, print a newline to get us
  697.      beyond the copyright line...  But errors should still set off
  698.      the error message with a (single) blank line.  */
  699.   if (!quiet)
  700.     printf_filtered ("\n");
  701.   error_pre_print = "\n";
  702.   warning_pre_print = "\nwarning: ";
  703.  
  704.   /* Set the initial language. */
  705.   {
  706.     struct partial_symtab *pst = find_main_psymtab ();
  707.     enum language lang = language_unknown;      
  708.     if (pst == NULL) ;
  709. #if 0
  710.     /* A better solution would set the language when reading the psymtab.
  711.        This would win for symbol file formats that encode the langauge,
  712.        such as DWARF.  But, we don't do that yet. FIXME */
  713.     else if (pst->language != language_unknown)
  714.     lang = pst->language;
  715. #endif
  716.     else if (pst->filename != NULL)
  717.       lang = deduce_language_from_filename (pst->filename);
  718.     if (lang == language_unknown) /* Make C the default language */
  719.     lang = language_c;
  720.     set_language (lang);
  721.   }
  722.  
  723.   if (corearg != NULL)
  724.     if (!setjmp (to_top_level))
  725.       core_file_command (corearg, !batch);
  726.     else if (isdigit (corearg[0]) && !setjmp (to_top_level))
  727.       attach_command (corearg, !batch);
  728.   do_cleanups (ALL_CLEANUPS);
  729.  
  730.   if (ttyarg != NULL)
  731.     if (!setjmp (to_top_level))
  732.       tty_command (ttyarg, !batch);
  733.   do_cleanups (ALL_CLEANUPS);
  734.  
  735. #ifdef ADDITIONAL_OPTION_HANDLER
  736.   ADDITIONAL_OPTION_HANDLER;
  737. #endif
  738.  
  739.   /* Error messages should no longer be distinguished with extra output. */
  740.   error_pre_print = 0;
  741.   warning_pre_print = "warning: ";
  742.  
  743.   {
  744.     struct stat homebuf, cwdbuf;
  745.     char *homedir, *homeinit;
  746.  
  747.     /* Read init file, if it exists in home directory  */
  748.     homedir = getenv ("HOME");
  749.     if (homedir)
  750.       {
  751.     homeinit = (char *) alloca (strlen (getenv ("HOME")) +
  752.                     strlen (gdbinit) + 10);
  753.     strcpy (homeinit, getenv ("HOME"));
  754.     strcat (homeinit, "/");
  755.     strcat (homeinit, gdbinit);
  756.     if (!inhibit_gdbinit && access (homeinit, R_OK) == 0)
  757.       if (!setjmp (to_top_level))
  758.         source_command (homeinit, 0);
  759.     do_cleanups (ALL_CLEANUPS);
  760.  
  761.     /* Do stats; no need to do them elsewhere since we'll only
  762.        need them if homedir is set.  Make sure that they are
  763.        zero in case one of them fails (this guarantees that they
  764.        won't match if either exists).  */
  765.     
  766.     memset (&homebuf, 0, sizeof (struct stat));
  767.     memset (&cwdbuf, 0, sizeof (struct stat));
  768.     
  769.     stat (homeinit, &homebuf);
  770.     stat (gdbinit, &cwdbuf); /* We'll only need this if
  771.                      homedir was set.  */
  772.       }
  773.     
  774.     /* Read the input file in the current directory, *if* it isn't
  775.        the same file (it should exist, also).  */
  776.  
  777.     if (!homedir
  778.     || bcmp ((char *) &homebuf,
  779.          (char *) &cwdbuf,
  780.          sizeof (struct stat)))
  781.       if (!inhibit_gdbinit && access (gdbinit, R_OK) == 0)
  782.     if (!setjmp (to_top_level))
  783.       source_command (gdbinit, 0);
  784.     do_cleanups (ALL_CLEANUPS);
  785.   }
  786.  
  787.   for (i = 0; i < ncmd; i++)
  788.     if (!setjmp (to_top_level))
  789.       {
  790.     if (cmdarg[i][0] == '-' && cmdarg[i][1] == '\0')
  791.       read_command_file (stdin);
  792.     else
  793.       source_command (cmdarg[i], !batch);
  794.     do_cleanups (ALL_CLEANUPS);
  795.       }
  796.   free ((PTR)cmdarg);
  797.  
  798.   /* Read in the old history after all the command files have been read. */
  799.   initialize_history();
  800.  
  801.   if (batch)
  802.     {
  803.       /* We have hit the end of the batch file.  */
  804.       exit (0);
  805.     }
  806.  
  807.   /* Do any host- or target-specific hacks.  This is used for i960 targets
  808.      to force the user to set a nindy target and spec its parameters.  */
  809.  
  810. #ifdef BEFORE_MAIN_LOOP_HOOK
  811.   BEFORE_MAIN_LOOP_HOOK;
  812. #endif
  813.  
  814.   /* The command loop.  */
  815.  
  816.   while (1)
  817.     {
  818.       if (!setjmp (to_top_level))
  819.     {
  820.       do_cleanups (ALL_CLEANUPS);        /* Do complete cleanup */
  821.       command_loop ();
  822.           quit_command ((char *)0, instream == stdin);
  823.     }
  824.     }
  825.   /* No exit -- exit is through quit_command.  */
  826. }
  827.  
  828. /* Execute the line P as a command.
  829.    Pass FROM_TTY as second argument to the defining function.  */
  830.  
  831. void
  832. execute_command (p, from_tty)
  833.      char *p;
  834.      int from_tty;
  835. {
  836.   register struct cmd_list_element *c;
  837.   register struct command_line *cmdlines;
  838.   register enum language flang;
  839.   static const struct language_defn *saved_language = 0;
  840.   static int warned = 0;
  841.  
  842.   free_all_values ();
  843.  
  844.   /* This can happen when command_line_input hits end of file.  */
  845.   if (p == NULL)
  846.       return;
  847.   
  848.   while (*p == ' ' || *p == '\t') p++;
  849.   if (*p)
  850.     {
  851.       char *arg;
  852.       
  853.       c = lookup_cmd (&p, cmdlist, "", 0, 1);
  854.       /* Pass null arg rather than an empty one.  */
  855.       arg = *p ? p : 0;
  856.       if (c->class == class_user)
  857.     {
  858.       struct cleanup *old_chain;
  859.       
  860.       if (*p)
  861.         error ("User-defined commands cannot take arguments.");
  862.       cmdlines = c->user_commands;
  863.       if (cmdlines == 0)
  864.         /* Null command */
  865.         return;
  866.  
  867.       /* Set the instream to 0, indicating execution of a
  868.          user-defined function.  */
  869.       old_chain =  make_cleanup (source_cleanup, instream);
  870.       instream = (FILE *) 0;
  871.       while (cmdlines)
  872.         {
  873.           execute_command (cmdlines->line, 0);
  874.           cmdlines = cmdlines->next;
  875.         }
  876.       do_cleanups (old_chain);
  877.     }
  878.       else if (c->type == set_cmd || c->type == show_cmd)
  879.     do_setshow_command (arg, from_tty & caution, c);
  880.       else if (c->function.cfunc == NO_FUNCTION)
  881.     error ("That is not a command, just a help topic.");
  882.       else
  883.     (*c->function.cfunc) (arg, from_tty & caution);
  884.    }
  885.  
  886.   /* Tell the user if the language has changed (except first time).  */
  887.   if (current_language != saved_language)
  888.   {
  889.     if (language_mode == language_mode_auto) {
  890.       if (saved_language)
  891.     language_info (1);    /* Print what changed.  */
  892.     }
  893.     saved_language = current_language;
  894.     warned = 0;
  895.   }
  896.  
  897.   /* Warn the user if the working language does not match the
  898.      language of the current frame.  Only warn the user if we are
  899.      actually running the program, i.e. there is a stack. */
  900.   /* FIXME:  This should be cacheing the frame and only running when
  901.      the frame changes.  */
  902.   if (target_has_stack)
  903.   {
  904.     flang = get_frame_language ();
  905.     if (!warned
  906.         && flang != language_unknown
  907.     && flang != current_language->la_language)
  908.     {
  909.       printf_filtered ("%s\n", lang_frame_mismatch_warn);
  910.       warned = 1;
  911.     }
  912.   }
  913. }
  914.  
  915. /* ARGSUSED */
  916. static void
  917. command_loop_marker (foo)
  918.      int foo;
  919. {
  920. }
  921.  
  922. /* Read commands from `instream' and execute them
  923.    until end of file or error reading instream.  */
  924. static void
  925. command_loop ()
  926. {
  927.   struct cleanup *old_chain;
  928.   char *command;
  929.   int stdin_is_tty = ISATTY (stdin);
  930.  
  931.   while (!feof (instream))
  932.     {
  933.       if (window_hook && instream == stdin)
  934.     (*window_hook) (instream, prompt);
  935.  
  936.       quit_flag = 0;
  937.       if (instream == stdin && stdin_is_tty)
  938.     reinitialize_more_filter ();
  939.       old_chain = make_cleanup (command_loop_marker, 0);
  940.       command = command_line_input (instream == stdin ? prompt : (char *) NULL,
  941.                       instream == stdin);
  942.       if (command == 0)
  943.     return;
  944.       execute_command (command, instream == stdin);
  945.       /* Do any commands attached to breakpoint we stopped at.  */
  946.       bpstat_do_actions (&stop_bpstat);
  947.       do_cleanups (old_chain);
  948.     }
  949. }
  950.  
  951. /* Commands call this if they do not want to be repeated by null lines.  */
  952.  
  953. void
  954. dont_repeat ()
  955. {
  956.   /* If we aren't reading from standard input, we are saving the last
  957.      thing read from stdin in line and don't want to delete it.  Null lines
  958.      won't repeat here in any case.  */
  959.   if (instream == stdin)
  960.     *line = 0;
  961. }
  962.  
  963. /* Read a line from the stream "instream" without command line editing.
  964.  
  965.    It prints PRROMPT once at the start.
  966.    Action is compatible with "readline", e.g. space for the result is 
  967.    malloc'd and should be freed by the caller.
  968.  
  969.    A NULL return means end of file.  */
  970. char *
  971. gdb_readline (prrompt)
  972.      char *prrompt;
  973. {
  974.   int c;
  975.   char *result;
  976.   int input_index = 0;
  977.   int result_size = 80;
  978.  
  979.   if (prrompt)
  980.     {
  981.       printf (prrompt);
  982.       fflush (stdout);
  983.     }
  984.   
  985.   result = (char *) xmalloc (result_size);
  986.  
  987.   while (1)
  988.     {
  989.       /* Read from stdin if we are executing a user defined command.
  990.      This is the right thing for prompt_for_continue, at least.  */
  991.       c = fgetc (instream ? instream : stdin);
  992.  
  993.       if (c == EOF)
  994.     {
  995.       free (result);
  996.       return NULL;
  997.     }
  998.  
  999.       if (c == '\n')
  1000.     break;
  1001.  
  1002.       result[input_index++] = c;
  1003.       while (input_index >= result_size)
  1004.     {
  1005.       result_size *= 2;
  1006.       result = (char *) xrealloc (result, result_size);
  1007.     }
  1008.     }
  1009.  
  1010.   result[input_index++] = '\0';
  1011.   return result;
  1012. }
  1013.  
  1014. /* Variables which control command line editing and history
  1015.    substitution.  These variables are given default values at the end
  1016.    of this file.  */
  1017. static int command_editing_p;
  1018. static int history_expansion_p;
  1019. static int write_history_p;
  1020. static int history_size;
  1021. static char *history_filename;
  1022.  
  1023. /* Variables which are necessary for fancy command line editing.  */
  1024. char *gdb_completer_word_break_characters =
  1025.   " \t\n!@#$%^&*()-+=|~`}{[]\"';:?/>.<,";
  1026.  
  1027. /* Functions that are used as part of the fancy command line editing.  */
  1028.  
  1029. /* This can be used for functions which don't want to complete on symbols
  1030.    but don't want to complete on anything else either.  */
  1031. /* ARGSUSED */
  1032. char **
  1033. noop_completer (text)
  1034.      char *text;
  1035. {
  1036.   return NULL;
  1037. }
  1038.  
  1039. /* Generate symbol names one by one for the completer.  If STATE is
  1040.    zero, then we need to initialize, otherwise the initialization has
  1041.    already taken place.  TEXT is what we expect the symbol to start
  1042.    with.  RL_LINE_BUFFER is available to be looked at; it contains the
  1043.    entire text of the line.  RL_POINT is the offset in that line of
  1044.    the cursor.  You should pretend that the line ends at RL_POINT.
  1045.    The result is NULL if there are no more completions, else a char
  1046.    string which is a possible completion.  */
  1047.  
  1048. static char *
  1049. symbol_completion_function (text, state)
  1050.      char *text;
  1051.      int state;
  1052. {
  1053.   static char **list = (char **)NULL;
  1054.   static int index;
  1055.   char *output;
  1056.   extern char *rl_line_buffer;
  1057.   extern int rl_point;
  1058.   char *tmp_command, *p;
  1059.   struct cmd_list_element *c, *result_list;
  1060.  
  1061.   if (!state)
  1062.     {
  1063.       /* Free the storage used by LIST, but not by the strings inside.  This is
  1064.      because rl_complete_internal () frees the strings. */
  1065.       if (list)
  1066.     free ((PTR)list);
  1067.       list = 0;
  1068.       index = 0;
  1069.  
  1070.       /* Decide whether to complete on a list of gdb commands or on
  1071.      symbols.  */
  1072.       tmp_command = (char *) alloca (rl_point + 1);
  1073.       p = tmp_command;
  1074.       
  1075.       strncpy (tmp_command, rl_line_buffer, rl_point);
  1076.       tmp_command[rl_point] = '\0';
  1077.  
  1078.       if (rl_point == 0)
  1079.     {
  1080.       /* An empty line we want to consider ambiguous; that is,
  1081.          it could be any command.  */
  1082.       c = (struct cmd_list_element *) -1;
  1083.       result_list = 0;
  1084.     }
  1085.       else
  1086.     c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
  1087.  
  1088.       /* Move p up to the next interesting thing.  */
  1089.       while (*p == ' ' || *p == '\t')
  1090.     p++;
  1091.  
  1092.       if (!c)
  1093.     /* He's typed something unrecognizable.  Sigh.  */
  1094.     list = (char **) 0;
  1095.       else if (c == (struct cmd_list_element *) -1)
  1096.     {
  1097.       /* If we didn't recognize everything up to the thing that
  1098.          needs completing, and we don't know what command it is
  1099.          yet, we are in trouble.  Part of the trouble might be
  1100.          that the list of delimiters used by readline includes
  1101.          '-', which we use in commands.  Check for this.  */
  1102.       if (p + strlen(text) != tmp_command + rl_point) {
  1103.         if (tmp_command[rl_point - strlen(text) - 1] == '-')
  1104.           text = p;
  1105.         else {
  1106.           /* This really should not produce an error.  Better would
  1107.          be to pretend to hit RETURN here; this would produce a
  1108.          response like "Ambiguous command: foo, foobar, etc",
  1109.          and leave the line available for re-entry with ^P.  Instead,
  1110.          this error blows away the user's typed input without
  1111.          any way to get it back.  */
  1112.           error ("  Unrecognized command.");
  1113.         }
  1114.       }
  1115.       
  1116.       /* He's typed something ambiguous.  This is easier.  */
  1117.       if (result_list)
  1118.         list = complete_on_cmdlist (*result_list->prefixlist, text);
  1119.       else
  1120.         list = complete_on_cmdlist (cmdlist, text);
  1121.     }
  1122.       else
  1123.     {
  1124.       /* If we've gotten this far, gdb has recognized a full
  1125.          command.  There are several possibilities:
  1126.  
  1127.          1) We need to complete on the command.
  1128.          2) We need to complete on the possibilities coming after
  1129.          the command.
  1130.          2) We need to complete the text of what comes after the
  1131.          command.   */
  1132.  
  1133.       if (!*p && *text)
  1134.         /* Always (might be longer versions of thie command).  */
  1135.         list = complete_on_cmdlist (result_list, text);
  1136.       else if (!*p && !*text)
  1137.         {
  1138.           if (c->prefixlist)
  1139.         list = complete_on_cmdlist (*c->prefixlist, "");
  1140.           else
  1141.         list = (*c->completer) ("");
  1142.         }
  1143.       else
  1144.         {
  1145.           if (c->prefixlist && !c->allow_unknown)
  1146.         {
  1147. #if 0
  1148.           /* Something like "info adsfkdj".  But error() is not
  1149.              the proper response; just return no completions
  1150.              instead.  */
  1151.           *p = '\0';
  1152.           error ("\"%s\" command requires a subcommand.",
  1153.              tmp_command);
  1154. #else
  1155.           list = NULL;
  1156. #endif
  1157.         }
  1158.           else
  1159.         list = (*c->completer) (text);
  1160.         }
  1161.     }
  1162.     }
  1163.  
  1164.   /* If the debugged program wasn't compiled with symbols, or if we're
  1165.      clearly completing on a command and no command matches, return
  1166.      NULL.  */
  1167.   if (!list)
  1168.     return ((char *)NULL);
  1169.  
  1170.   output = list[index];
  1171.   if (output)
  1172.     index++;
  1173.  
  1174.   return (output);
  1175. }
  1176.  
  1177. #ifdef STOP_SIGNAL
  1178. static void
  1179. stop_sig (signo)
  1180. int signo;
  1181. {
  1182. #if STOP_SIGNAL == SIGTSTP
  1183.   signal (SIGTSTP, SIG_DFL);
  1184.   sigsetmask (0);
  1185.   kill (getpid (), SIGTSTP);
  1186.   signal (SIGTSTP, stop_sig);
  1187. #else
  1188.   signal (STOP_SIGNAL, stop_sig);
  1189. #endif
  1190.   printf ("%s", prompt);
  1191.   fflush (stdout);
  1192.  
  1193.   /* Forget about any previous command -- null line now will do nothing.  */
  1194.   dont_repeat ();
  1195. }
  1196. #endif /* STOP_SIGNAL */
  1197.  
  1198. /* Initialize signal handlers. */
  1199. static void
  1200. do_nothing (signo)
  1201. int signo;
  1202. {
  1203. }
  1204.  
  1205. static void
  1206. init_signals ()
  1207. {
  1208.   signal (SIGINT, request_quit);
  1209.  
  1210.   /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
  1211.      passed to the inferior, which we don't want.  It would be
  1212.      possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
  1213.      on BSD4.3 systems using vfork, that can affect the
  1214.      GDB process as well as the inferior (the signal handling tables
  1215.      might be in memory, shared between the two).  Since we establish
  1216.      a handler for SIGQUIT, when we call exec it will set the signal
  1217.      to SIG_DFL for us.  */
  1218.   signal (SIGQUIT, do_nothing);
  1219.   if (signal (SIGHUP, do_nothing) != SIG_IGN)
  1220.     signal (SIGHUP, disconnect);
  1221.   signal (SIGFPE, float_handler);
  1222.  
  1223. #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
  1224.   signal (SIGWINCH, SIGWINCH_HANDLER);
  1225. #endif
  1226. }
  1227.  
  1228. /* Read one line from the command input stream `instream'
  1229.    into the local static buffer `linebuffer' (whose current length
  1230.    is `linelength').
  1231.    The buffer is made bigger as necessary.
  1232.    Returns the address of the start of the line.
  1233.  
  1234.    NULL is returned for end of file.
  1235.  
  1236.    *If* the instream == stdin & stdin is a terminal, the line read
  1237.    is copied into the file line saver (global var char *line,
  1238.    length linesize) so that it can be duplicated.
  1239.  
  1240.    This routine either uses fancy command line editing or
  1241.    simple input as the user has requested.  */
  1242.  
  1243. char *
  1244. command_line_input (prrompt, repeat)
  1245.      char *prrompt;
  1246.      int repeat;
  1247. {
  1248.   static char *linebuffer = 0;
  1249.   static unsigned linelength = 0;
  1250.   register char *p;
  1251.   char *p1;
  1252.   char *rl;
  1253.   char *local_prompt = prrompt;
  1254.   register int c;
  1255.   char *nline;
  1256.   char got_eof = 0;
  1257.  
  1258.   if (linebuffer == 0)
  1259.     {
  1260.       linelength = 80;
  1261.       linebuffer = (char *) xmalloc (linelength);
  1262.     }
  1263.  
  1264.   p = linebuffer;
  1265.  
  1266.   /* Control-C quits instantly if typed while in this loop
  1267.      since it should not wait until the user types a newline.  */
  1268.   immediate_quit++;
  1269. #ifdef STOP_SIGNAL
  1270.   signal (STOP_SIGNAL, stop_sig);
  1271. #endif
  1272.  
  1273.   while (1)
  1274.     {
  1275.       /* Reports are that some Sys V's don't flush stdout/err on reads
  1276.      from stdin, when stdin/out are sockets rather than ttys.  So we
  1277.      have to do it ourselves, to make emacs-gdb and xxgdb work.
  1278.      On other machines, doing this once per input should be a cheap nop.  */
  1279.       fflush (stdout);
  1280.       fflush (stderr);
  1281.  
  1282.       /* Don't use fancy stuff if not talking to stdin.  */
  1283.       if (command_editing_p && instream == stdin
  1284.       && ISATTY (instream))
  1285.     rl = readline (local_prompt);
  1286.       else
  1287.     rl = gdb_readline (local_prompt);
  1288.  
  1289.       if (!rl || rl == (char *) EOF)
  1290.     {
  1291.       got_eof = 1;
  1292.       break;
  1293.     }
  1294.       if (strlen(rl) + 1 + (p - linebuffer) > linelength)
  1295.     {
  1296.       linelength = strlen(rl) + 1 + (p - linebuffer);
  1297.       nline = (char *) xrealloc (linebuffer, linelength);
  1298.       p += nline - linebuffer;
  1299.       linebuffer = nline;
  1300.     }
  1301.       p1 = rl;
  1302.       /* Copy line.  Don't copy null at end.  (Leaves line alone
  1303.          if this was just a newline)  */
  1304.       while (*p1)
  1305.     *p++ = *p1++;
  1306.  
  1307.       free (rl);            /* Allocated in readline.  */
  1308.  
  1309.       if (p == linebuffer || *(p - 1) != '\\')
  1310.     break;
  1311.  
  1312.       p--;            /* Put on top of '\'.  */
  1313.       local_prompt = (char *) 0;
  1314.   }
  1315.  
  1316. #ifdef STOP_SIGNAL
  1317.   signal (SIGTSTP, SIG_DFL);
  1318. #endif
  1319.   immediate_quit--;
  1320.  
  1321.   if (got_eof)
  1322.     return NULL;
  1323.  
  1324.   /* Do history expansion if that is wished.  */
  1325.   if (history_expansion_p && instream == stdin
  1326.       && ISATTY (instream))
  1327.     {
  1328.       char *history_value;
  1329.       int expanded;
  1330.  
  1331.       *p = '\0';        /* Insert null now.  */
  1332.       expanded = history_expand (linebuffer, &history_value);
  1333.       if (expanded)
  1334.     {
  1335.       /* Print the changes.  */
  1336.       printf ("%s\n", history_value);
  1337.  
  1338.       /* If there was an error, call this function again.  */
  1339.       if (expanded < 0)
  1340.         {
  1341.           free (history_value);
  1342.           return command_line_input (prrompt, repeat);
  1343.         }
  1344.       if (strlen (history_value) > linelength)
  1345.         {
  1346.           linelength = strlen (history_value) + 1;
  1347.           linebuffer = (char *) xrealloc (linebuffer, linelength);
  1348.         }
  1349.       strcpy (linebuffer, history_value);
  1350.       p = linebuffer + strlen(linebuffer);
  1351.       free (history_value);
  1352.     }
  1353.     }
  1354.  
  1355.   /* If we just got an empty line, and that is supposed
  1356.      to repeat the previous command, return the value in the
  1357.      global buffer.  */
  1358.   if (repeat)
  1359.     {
  1360.       if (p == linebuffer)
  1361.     return line;
  1362.       p1 = linebuffer;
  1363.       while (*p1 == ' ' || *p1 == '\t')
  1364.     p1++;
  1365.       if (!*p1)
  1366.     return line;
  1367.     }
  1368.  
  1369.   *p = 0;
  1370.  
  1371.   /* Add line to history if appropriate.  */
  1372.   if (instream == stdin
  1373.       && ISATTY (stdin) && *linebuffer)
  1374.     add_history (linebuffer);
  1375.  
  1376.   /* Note: lines consisting soley of comments are added to the command
  1377.      history.  This is useful when you type a command, and then
  1378.      realize you don't want to execute it quite yet.  You can comment
  1379.      out the command and then later fetch it from the value history
  1380.      and remove the '#'.  The kill ring is probably better, but some
  1381.      people are in the habit of commenting things out.  */
  1382.   p1 = linebuffer;
  1383.   while ((c = *p1++) != '\0')
  1384.     {
  1385.       if (c == '"')
  1386.     while ((c = *p1++) != '"')
  1387.       {
  1388.         /* Make sure an escaped '"' doesn't make us think the string
  1389.            is ended.  */
  1390.         if (c == '\\')
  1391.           parse_escape (&p1);
  1392.         if (c == '\0')
  1393.           break;
  1394.       }
  1395.       else if (c == '\'')
  1396.     while ((c = *p1++) != '\'')
  1397.       {
  1398.         /* Make sure an escaped '\'' doesn't make us think the string
  1399.            is ended.  */
  1400.         if (c == '\\')
  1401.           parse_escape (&p1);
  1402.         if (c == '\0')
  1403.           break;
  1404.       }
  1405.       else if (c == '#')
  1406.     {
  1407.       /* Found a comment.  */
  1408.       p1[-1] = '\0';
  1409.       break;
  1410.     }
  1411.     }
  1412.  
  1413.   /* Save into global buffer if appropriate.  */
  1414.   if (repeat)
  1415.     {
  1416.       if (linelength > linesize)
  1417.     {
  1418.       line = xrealloc (line, linelength);
  1419.       linesize = linelength;
  1420.     }
  1421.       strcpy (line, linebuffer);
  1422.       return line;
  1423.     }
  1424.  
  1425.   return linebuffer;
  1426. }
  1427.  
  1428. /* Read lines from the input stream
  1429.    and accumulate them in a chain of struct command_line's
  1430.    which is then returned.  */
  1431.  
  1432. struct command_line *
  1433. read_command_lines ()
  1434. {
  1435.   struct command_line *first = 0;
  1436.   register struct command_line *next, *tail = 0;
  1437.   register char *p, *p1;
  1438.   struct cleanup *old_chain = 0;
  1439.  
  1440.   while (1)
  1441.     {
  1442.       dont_repeat ();
  1443.       p = command_line_input ((char *) NULL, instream == stdin);
  1444.       if (p == NULL)
  1445.     /* Treat end of file like "end".  */
  1446.     break;
  1447.       
  1448.       /* Remove leading and trailing blanks.  */
  1449.       while (*p == ' ' || *p == '\t') p++;
  1450.       p1 = p + strlen (p);
  1451.       while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t')) p1--;
  1452.  
  1453.       /* Is this "end"?  */
  1454.       if (p1 - p == 3 && !strncmp (p, "end", 3))
  1455.     break;
  1456.  
  1457.       /* No => add this line to the chain of command lines.  */
  1458.       next = (struct command_line *) xmalloc (sizeof (struct command_line));
  1459.       next->line = savestring (p, p1 - p);
  1460.       next->next = 0;
  1461.       if (tail)
  1462.     {
  1463.       tail->next = next;
  1464.     }
  1465.       else
  1466.     {
  1467.       /* We just read the first line.
  1468.          From now on, arrange to throw away the lines we have
  1469.          if we quit or get an error while inside this function.  */
  1470.       first = next;
  1471.       old_chain = make_cleanup (free_command_lines, &first);
  1472.     }
  1473.       tail = next;
  1474.     }
  1475.  
  1476.   dont_repeat ();
  1477.  
  1478.   /* Now we are about to return the chain to our caller,
  1479.      so freeing it becomes his responsibility.  */
  1480.   if (first)
  1481.     discard_cleanups (old_chain);
  1482.   return first;
  1483. }
  1484.  
  1485. /* Free a chain of struct command_line's.  */
  1486.  
  1487. void
  1488. free_command_lines (lptr)
  1489.       struct command_line **lptr;
  1490. {
  1491.   register struct command_line *l = *lptr;
  1492.   register struct command_line *next;
  1493.  
  1494.   while (l)
  1495.     {
  1496.       next = l->next;
  1497.       free (l->line);
  1498.       free ((PTR)l);
  1499.       l = next;
  1500.     }
  1501. }
  1502.  
  1503. /* Add an element to the list of info subcommands.  */
  1504.  
  1505. void
  1506. add_info (name, fun, doc)
  1507.      char *name;
  1508.      void (*fun) PARAMS ((char *, int));
  1509.      char *doc;
  1510. {
  1511.   add_cmd (name, no_class, fun, doc, &infolist);
  1512. }
  1513.  
  1514. /* Add an alias to the list of info subcommands.  */
  1515.  
  1516. void
  1517. add_info_alias (name, oldname, abbrev_flag)
  1518.      char *name;
  1519.      char *oldname;
  1520.      int abbrev_flag;
  1521. {
  1522.   add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist);
  1523. }
  1524.  
  1525. /* The "info" command is defined as a prefix, with allow_unknown = 0.
  1526.    Therefore, its own definition is called only for "info" with no args.  */
  1527.  
  1528. /* ARGSUSED */
  1529. static void
  1530. info_command (arg, from_tty)
  1531.      char *arg;
  1532.      int from_tty;
  1533. {
  1534.   printf ("\"info\" must be followed by the name of an info command.\n");
  1535.   help_list (infolist, "info ", -1, stdout);
  1536. }
  1537.  
  1538. /* The "show" command with no arguments shows all the settings.  */
  1539.  
  1540. /* ARGSUSED */
  1541. static void
  1542. show_command (arg, from_tty)
  1543.      char *arg;
  1544.      int from_tty;
  1545. {
  1546.   cmd_show_list (showlist, from_tty, "");
  1547. }
  1548.  
  1549. /* Add an element to the list of commands.  */
  1550.  
  1551. void
  1552. add_com (name, class, fun, doc)
  1553.      char *name;
  1554.      enum command_class class;
  1555.      void (*fun) PARAMS ((char *, int));
  1556.      char *doc;
  1557. {
  1558.   add_cmd (name, class, fun, doc, &cmdlist);
  1559. }
  1560.  
  1561. /* Add an alias or abbreviation command to the list of commands.  */
  1562.  
  1563. void
  1564. add_com_alias (name, oldname, class, abbrev_flag)
  1565.      char *name;
  1566.      char *oldname;
  1567.      enum command_class class;
  1568.      int abbrev_flag;
  1569. {
  1570.   add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist);
  1571. }
  1572.  
  1573. void
  1574. error_no_arg (why)
  1575.      char *why;
  1576. {
  1577.   error ("Argument required (%s).", why);
  1578. }
  1579.  
  1580. /* ARGSUSED */
  1581. static void
  1582. help_command (command, from_tty)
  1583.      char *command;
  1584.      int from_tty; /* Ignored */
  1585. {
  1586.   help_cmd (command, stdout);
  1587. }
  1588.  
  1589. static void
  1590. validate_comname (comname)
  1591.      char *comname;
  1592. {
  1593.   register char *p;
  1594.  
  1595.   if (comname == 0)
  1596.     error_no_arg ("name of command to define");
  1597.  
  1598.   p = comname;
  1599.   while (*p)
  1600.     {
  1601.       if (!isalnum(*p) && *p != '-')
  1602.     error ("Junk in argument list: \"%s\"", p);
  1603.       p++;
  1604.     }
  1605. }
  1606.  
  1607. /* This is just a placeholder in the command data structures.  */
  1608. static void
  1609. user_defined_command (ignore, from_tty)
  1610.      char *ignore;
  1611.      int from_tty;
  1612. {
  1613. }
  1614.  
  1615. static void
  1616. define_command (comname, from_tty)
  1617.      char *comname;
  1618.      int from_tty;
  1619. {
  1620.   register struct command_line *cmds;
  1621.   register struct cmd_list_element *c, *newc;
  1622.   char *tem = comname;
  1623.  
  1624.   validate_comname (comname);
  1625.  
  1626.   /* Look it up, and verify that we got an exact match.  */
  1627.   c = lookup_cmd (&tem, cmdlist, "", -1, 1);
  1628.   if (c && 0 != strcmp (comname, c->name))
  1629.     c = 0;
  1630.     
  1631.   if (c)
  1632.     {
  1633.       if (c->class == class_user || c->class == class_alias)
  1634.     tem = "Redefine command \"%s\"? ";
  1635.       else
  1636.     tem = "Really redefine built-in command \"%s\"? ";
  1637.       if (!query (tem, c->name))
  1638.     error ("Command \"%s\" not redefined.", c->name);
  1639.     }
  1640.  
  1641.   comname = savestring (comname, strlen (comname));
  1642.  
  1643.   /* If the rest of the commands will be case insensetive, this one 
  1644.      should behave in the same manner. */
  1645.   for (tem = comname; *tem; tem++)
  1646.     if (isupper(*tem)) *tem = tolower(*tem);
  1647.  
  1648.   if (from_tty)
  1649.     {
  1650.       printf ("Type commands for definition of \"%s\".\n\
  1651. End with a line saying just \"end\".\n", comname);
  1652.       fflush (stdout);
  1653.     }
  1654.  
  1655.   cmds = read_command_lines ();
  1656.  
  1657.   if (c && c->class == class_user)
  1658.     free_command_lines (&c->user_commands);
  1659.  
  1660.   newc = add_cmd (comname, class_user, user_defined_command,
  1661.        (c && c->class == class_user)
  1662.        ? c->doc : savestring ("User-defined.", 13), &cmdlist);
  1663.   newc->user_commands = cmds;
  1664. }
  1665.  
  1666. static void
  1667. document_command (comname, from_tty)
  1668.      char *comname;
  1669.      int from_tty;
  1670. {
  1671.   struct command_line *doclines;
  1672.   register struct cmd_list_element *c;
  1673.   char *tem = comname;
  1674.  
  1675.   validate_comname (comname);
  1676.  
  1677.   c = lookup_cmd (&tem, cmdlist, "", 0, 1);
  1678.  
  1679.   if (c->class != class_user)
  1680.     error ("Command \"%s\" is built-in.", comname);
  1681.  
  1682.   if (from_tty)
  1683.     printf ("Type documentation for \"%s\".\n\
  1684. End with a line saying just \"end\".\n", comname);
  1685.  
  1686.   doclines = read_command_lines ();
  1687.  
  1688.   if (c->doc) free (c->doc);
  1689.  
  1690.   {
  1691.     register struct command_line *cl1;
  1692.     register int len = 0;
  1693.  
  1694.     for (cl1 = doclines; cl1; cl1 = cl1->next)
  1695.       len += strlen (cl1->line) + 1;
  1696.  
  1697.     c->doc = (char *) xmalloc (len + 1);
  1698.     *c->doc = 0;
  1699.  
  1700.     for (cl1 = doclines; cl1; cl1 = cl1->next)
  1701.       {
  1702.     strcat (c->doc, cl1->line);
  1703.     if (cl1->next)
  1704.       strcat (c->doc, "\n");
  1705.       }
  1706.   }
  1707.  
  1708.   free_command_lines (&doclines);
  1709. }
  1710.  
  1711. static void
  1712. print_gnu_advertisement()
  1713. {
  1714.     printf ("\
  1715. GDB is free software and you are welcome to distribute copies of it\n\
  1716.  under certain conditions; type \"show copying\" to see the conditions.\n\
  1717. There is absolutely no warranty for GDB; type \"show warranty\" for details.\n\
  1718. ");
  1719. }
  1720.  
  1721. static void
  1722. print_gdb_version ()
  1723. {
  1724.   printf_filtered ("\
  1725. GDB %s, Copyright 1992 Free Software Foundation, Inc.",
  1726.       version);
  1727. }
  1728.  
  1729. /* ARGSUSED */
  1730. static void
  1731. show_version (args, from_tty)
  1732.      char *args;
  1733.      int from_tty;
  1734. {
  1735.   immediate_quit++;
  1736.   print_gnu_advertisement ();
  1737.   print_gdb_version ();
  1738.   printf_filtered ("\n");
  1739.   immediate_quit--;
  1740. }
  1741.  
  1742. /* xgdb calls this to reprint the usual GDB prompt.  */
  1743.  
  1744. void
  1745. print_prompt ()
  1746. {
  1747.   printf ("%s", prompt);
  1748.   fflush (stdout);
  1749. }
  1750.  
  1751. static void
  1752. quit_command (args, from_tty)
  1753.      char *args;
  1754.      int from_tty;
  1755. {
  1756.   if (inferior_pid != 0 && target_has_execution)
  1757.     {
  1758.       if (query ("The program is running.  Quit anyway? "))
  1759.     {
  1760.       target_kill ();
  1761.     }
  1762.       else
  1763.     error ("Not confirmed.");
  1764.     }
  1765.   /* Save the history information if it is appropriate to do so.  */
  1766.   if (write_history_p && history_filename)
  1767.     write_history (history_filename);
  1768.   exit (0);
  1769. }
  1770.  
  1771. /* Returns whether GDB is running on a terminal and whether the user
  1772.    desires that questions be asked of them on that terminal.  */
  1773.  
  1774. int
  1775. input_from_terminal_p ()
  1776. {
  1777.   return gdb_has_a_terminal && (instream == stdin) & caution;
  1778. }
  1779.  
  1780. /* ARGSUSED */
  1781. static void
  1782. pwd_command (args, from_tty)
  1783.      char *args;
  1784.      int from_tty;
  1785. {
  1786.   if (args) error ("The \"pwd\" command does not take an argument: %s", args);
  1787.   getcwd (dirbuf, sizeof (dirbuf));
  1788.  
  1789.   if (strcmp (dirbuf, current_directory))
  1790.     printf ("Working directory %s\n (canonically %s).\n",
  1791.         current_directory, dirbuf);
  1792.   else
  1793.     printf ("Working directory %s.\n", current_directory);
  1794. }
  1795.  
  1796. static void
  1797. cd_command (dir, from_tty)
  1798.      char *dir;
  1799.      int from_tty;
  1800. {
  1801.   int len;
  1802.   int change;
  1803.  
  1804.   /* If the new directory is absolute, repeat is a no-op; if relative,
  1805.      repeat might be useful but is more likely to be a mistake.  */
  1806.   dont_repeat ();
  1807.  
  1808.   if (dir == 0)
  1809.     error_no_arg ("new working directory");
  1810.  
  1811.   dir = tilde_expand (dir);
  1812.   make_cleanup (free, dir);
  1813.  
  1814.   if (chdir (dir) < 0)
  1815.     perror_with_name (dir);
  1816.  
  1817.   len = strlen (dir);
  1818.   dir = savestring (dir, len - (len > 1 && dir[len-1] == '/'));
  1819.   if (dir[0] == '/')
  1820.     current_directory = dir;
  1821.   else
  1822.     {
  1823.       current_directory = concat (current_directory, "/", dir, NULL);
  1824.       free (dir);
  1825.     }
  1826.  
  1827.   /* Now simplify any occurrences of `.' and `..' in the pathname.  */
  1828.  
  1829.   change = 1;
  1830.   while (change)
  1831.     {
  1832.       char *p;
  1833.       change = 0;
  1834.  
  1835.       for (p = current_directory; *p;)
  1836.     {
  1837.       if (!strncmp (p, "/./", 2)
  1838.           && (p[2] == 0 || p[2] == '/'))
  1839.         strcpy (p, p + 2);
  1840.       else if (!strncmp (p, "/..", 3)
  1841.            && (p[3] == 0 || p[3] == '/')
  1842.            && p != current_directory)
  1843.         {
  1844.           char *q = p;
  1845.           while (q != current_directory && q[-1] != '/') q--;
  1846.           if (q != current_directory)
  1847.         {
  1848.           strcpy (q-1, p+3);
  1849.           p = q-1;
  1850.         }
  1851.         }
  1852.       else p++;
  1853.     }
  1854.     }
  1855.  
  1856.   forget_cached_source_info ();
  1857.  
  1858.   if (from_tty)
  1859.     pwd_command ((char *) 0, 1);
  1860. }
  1861.  
  1862. /* ARGSUSED */
  1863. static void
  1864. source_command (args, from_tty)
  1865.      char *args;
  1866.      int from_tty;
  1867. {
  1868.   FILE *stream;
  1869.   struct cleanup *cleanups;
  1870.   char *file = args;
  1871.  
  1872.   if (file == 0)
  1873.     /* Let source without arguments read .gdbinit.  */
  1874.     file = gdbinit;
  1875.  
  1876.   file = tilde_expand (file);
  1877.   make_cleanup (free, file);
  1878.  
  1879.   stream = fopen (file, "r");
  1880.   if (stream == 0)
  1881.     perror_with_name (file);
  1882.  
  1883.   cleanups = make_cleanup (fclose, stream);
  1884.  
  1885.   read_command_file (stream);
  1886.  
  1887.   do_cleanups (cleanups);
  1888. }
  1889.  
  1890. /* ARGSUSED */
  1891. static void
  1892. echo_command (text, from_tty)
  1893.      char *text;
  1894.      int from_tty;
  1895. {
  1896.   char *p = text;
  1897.   register int c;
  1898.  
  1899.   if (text)
  1900.     while (c = *p++)
  1901.       {
  1902.     if (c == '\\')
  1903.       {
  1904.         /* \ at end of argument is used after spaces
  1905.            so they won't be lost.  */
  1906.         if (*p == 0)
  1907.           return;
  1908.  
  1909.         c = parse_escape (&p);
  1910.         if (c >= 0)
  1911.           printf_filtered ("%c", c);
  1912.       }
  1913.     else
  1914.       printf_filtered ("%c", c);
  1915.       }
  1916.  
  1917.   /* Force this output to appear now.  */
  1918.   wrap_here ("");
  1919.   fflush (stdout);
  1920. }
  1921.  
  1922. /* ARGSUSED */
  1923. static void
  1924. dump_me_command (args, from_tty)
  1925.      char *args;
  1926.      int from_tty;
  1927. {
  1928.   if (query ("Should GDB dump core? "))
  1929.     {
  1930.       signal (SIGQUIT, SIG_DFL);
  1931.       kill (getpid (), SIGQUIT);
  1932.     }
  1933. }
  1934.  
  1935. /* Functions to manipulate command line editing control variables.  */
  1936.  
  1937. /* Number of commands to print in each call to show_commands.  */
  1938. #define Hist_print 10
  1939. static void
  1940. show_commands (args, from_tty)
  1941.      char *args;
  1942.      int from_tty;
  1943. {
  1944.   /* Index for history commands.  Relative to history_base.  */
  1945.   int offset;
  1946.  
  1947.   /* Number of the history entry which we are planning to display next.
  1948.      Relative to history_base.  */
  1949.   static int num = 0;
  1950.  
  1951.   /* The first command in the history which doesn't exist (i.e. one more
  1952.      than the number of the last command).  Relative to history_base.  */
  1953.   int hist_len;
  1954.  
  1955.   extern struct _hist_entry *history_get PARAMS ((int));
  1956.   extern int history_base;
  1957.  
  1958.   /* Print out some of the commands from the command history.  */
  1959.   /* First determine the length of the history list.  */
  1960.   hist_len = history_size;
  1961.   for (offset = 0; offset < history_size; offset++)
  1962.     {
  1963.       if (!history_get (history_base + offset))
  1964.     {
  1965.       hist_len = offset;
  1966.       break;
  1967.     }
  1968.     }
  1969.  
  1970.   if (args)
  1971.     {
  1972.       if (args[0] == '+' && args[1] == '\0')
  1973.     /* "info editing +" should print from the stored position.  */
  1974.     ;
  1975.       else
  1976.     /* "info editing <exp>" should print around command number <exp>.  */
  1977.     num = (parse_and_eval_address (args) - history_base) - Hist_print / 2;
  1978.     }
  1979.   /* "show commands" means print the last Hist_print commands.  */
  1980.   else
  1981.     {
  1982.       num = hist_len - Hist_print;
  1983.     }
  1984.  
  1985.   if (num < 0)
  1986.     num = 0;
  1987.  
  1988.   /* If there are at least Hist_print commands, we want to display the last
  1989.      Hist_print rather than, say, the last 6.  */
  1990.   if (hist_len - num < Hist_print)
  1991.     {
  1992.       num = hist_len - Hist_print;
  1993.       if (num < 0)
  1994.     num = 0;
  1995.     }
  1996.  
  1997.   for (offset = num; offset < num + Hist_print && offset < hist_len; offset++)
  1998.     {
  1999.       printf_filtered ("%5d  %s\n", history_base + offset,
  2000.           (history_get (history_base + offset))->line);
  2001.     }
  2002.  
  2003.   /* The next command we want to display is the next one that we haven't
  2004.      displayed yet.  */
  2005.   num += Hist_print;
  2006.   
  2007.   /* If the user repeats this command with return, it should do what
  2008.      "show commands +" does.  This is unnecessary if arg is null,
  2009.      because "show commands +" is not useful after "show commands".  */
  2010.   if (from_tty && args)
  2011.     {
  2012.       args[0] = '+';
  2013.       args[1] = '\0';
  2014.     }
  2015. }
  2016.  
  2017. /* Called by do_setshow_command.  */
  2018. /* ARGSUSED */
  2019. static void
  2020. set_history_size_command (args, from_tty, c)
  2021.      char *args;
  2022.      int from_tty;
  2023.      struct cmd_list_element *c;
  2024. {
  2025.   if (history_size == UINT_MAX)
  2026.     unstifle_history ();
  2027.   else if (history_size >= 0)
  2028.     stifle_history (history_size);
  2029.   else
  2030.     {
  2031.       history_size = UINT_MAX;
  2032.       error ("History size must be non-negative");
  2033.     }
  2034. }
  2035.  
  2036. /* ARGSUSED */
  2037. static void
  2038. set_history (args, from_tty)
  2039.      char *args;
  2040.      int from_tty;
  2041. {
  2042.   printf ("\"set history\" must be followed by the name of a history subcommand.\n");
  2043.   help_list (sethistlist, "set history ", -1, stdout);
  2044. }
  2045.  
  2046. /* ARGSUSED */
  2047. static void
  2048. show_history (args, from_tty)
  2049.      char *args;
  2050.      int from_tty;
  2051. {
  2052.   cmd_show_list (showhistlist, from_tty, "");
  2053. }
  2054.  
  2055. int info_verbose = 0;        /* Default verbose msgs off */
  2056.  
  2057. /* Called by do_setshow_command.  An elaborate joke.  */
  2058. /* ARGSUSED */
  2059. static void 
  2060. set_verbose (args, from_tty, c)
  2061.      char *args;
  2062.      int from_tty;
  2063.      struct cmd_list_element *c;
  2064. {
  2065.   char *cmdname = "verbose";
  2066.   struct cmd_list_element *showcmd;
  2067.   
  2068.   showcmd = lookup_cmd_1 (&cmdname, showlist, NULL, 1);
  2069.  
  2070.   if (info_verbose)
  2071.     {
  2072.       c->doc = "Set verbose printing of informational messages.";
  2073.       showcmd->doc = "Show verbose printing of informational messages.";
  2074.     }
  2075.   else
  2076.     {
  2077.       c->doc = "Set verbosity.";
  2078.       showcmd->doc = "Show verbosity.";
  2079.     }
  2080. }
  2081.  
  2082. static void
  2083. float_handler (signo)
  2084. int signo;
  2085. {
  2086.   /* This message is based on ANSI C, section 4.7.  Note that integer
  2087.      divide by zero causes this, so "float" is a misnomer.  */
  2088.   error ("Erroneous arithmetic operation.");
  2089. }
  2090.  
  2091. /* Return whether we are running a batch file or from terminal.  */
  2092. int
  2093. batch_mode ()
  2094. {
  2095.   return !(instream == stdin && ISATTY (stdin));
  2096. }
  2097.  
  2098.  
  2099. static void
  2100. initialize_cmd_lists ()
  2101. {
  2102.   cmdlist = (struct cmd_list_element *) 0;
  2103.   infolist = (struct cmd_list_element *) 0;
  2104.   enablelist = (struct cmd_list_element *) 0;
  2105.   disablelist = (struct cmd_list_element *) 0;
  2106.   deletelist = (struct cmd_list_element *) 0;
  2107.   enablebreaklist = (struct cmd_list_element *) 0;
  2108.   setlist = (struct cmd_list_element *) 0;
  2109.   showlist = NULL;
  2110.   sethistlist = (struct cmd_list_element *) 0;
  2111.   showhistlist = NULL;
  2112.   unsethistlist = (struct cmd_list_element *) 0;
  2113. }
  2114.  
  2115. /* Init the history buffer.  Note that we are called after the init file(s)
  2116.  * have been read so that the user can change the history file via his
  2117.  * .gdbinit file (for instance).  The GDBHISTFILE environment variable
  2118.  * overrides all of this.
  2119.  */
  2120.  
  2121. static void
  2122. initialize_history()
  2123. {
  2124.   char *tmpenv;
  2125.  
  2126.   tmpenv = getenv ("HISTSIZE");
  2127.   if (tmpenv)
  2128.     history_size = atoi (tmpenv);
  2129.   else if (!history_size)
  2130.     history_size = 256;
  2131.  
  2132.   stifle_history (history_size);
  2133.  
  2134.   tmpenv = getenv ("GDBHISTFILE");
  2135.   if (tmpenv)
  2136.     history_filename = savestring (tmpenv, strlen(tmpenv));
  2137.   else if (!history_filename) {
  2138.     /* We include the current directory so that if the user changes
  2139.        directories the file written will be the same as the one
  2140.        that was read.  */
  2141.     history_filename = concat (current_directory, "/.gdb_history", NULL);
  2142.   }
  2143.   read_history (history_filename);
  2144. }
  2145.  
  2146. static void
  2147. initialize_main ()
  2148. {
  2149.   struct cmd_list_element *c;
  2150.   
  2151. #ifdef DEFAULT_PROMPT
  2152.   prompt = savestring (DEFAULT_PROMPT, strlen(DEFAULT_PROMPT));
  2153. #else
  2154.   prompt = savestring ("(gdb) ", 6);
  2155. #endif
  2156.  
  2157.   /* Set the important stuff up for command editing.  */
  2158.   command_editing_p = 1;
  2159.   history_expansion_p = 0;
  2160.   write_history_p = 0;
  2161.   
  2162.   /* Setup important stuff for command line editing.  */
  2163.   rl_completion_entry_function = (int (*)()) symbol_completion_function;
  2164.   rl_completer_word_break_characters = gdb_completer_word_break_characters;
  2165.   rl_readline_name = "gdb";
  2166.  
  2167.   /* Define the classes of commands.
  2168.      They will appear in the help list in the reverse of this order.  */
  2169.  
  2170.   add_cmd ("obscure", class_obscure, NO_FUNCTION, "Obscure features.", &cmdlist);
  2171.   add_cmd ("aliases", class_alias, NO_FUNCTION, "Aliases of other commands.", &cmdlist);
  2172.   add_cmd ("user-defined", class_user, NO_FUNCTION, "User-defined commands.\n\
  2173. The commands in this class are those defined by the user.\n\
  2174. Use the \"define\" command to define a command.", &cmdlist);
  2175.   add_cmd ("support", class_support, NO_FUNCTION, "Support facilities.", &cmdlist);
  2176.   add_cmd ("status", class_info, NO_FUNCTION, "Status inquiries.", &cmdlist);
  2177.   add_cmd ("files", class_files, NO_FUNCTION, "Specifying and examining files.", &cmdlist);
  2178.   add_cmd ("breakpoints", class_breakpoint, NO_FUNCTION, "Making program stop at certain points.", &cmdlist);
  2179.   add_cmd ("data", class_vars, NO_FUNCTION, "Examining data.", &cmdlist);
  2180.   add_cmd ("stack", class_stack, NO_FUNCTION, "Examining the stack.\n\
  2181. The stack is made up of stack frames.  Gdb assigns numbers to stack frames\n\
  2182. counting from zero for the innermost (currently executing) frame.\n\n\
  2183. At any time gdb identifies one frame as the \"selected\" frame.\n\
  2184. Variable lookups are done with respect to the selected frame.\n\
  2185. When the program being debugged stops, gdb selects the innermost frame.\n\
  2186. The commands below can be used to select other frames by number or address.",
  2187.        &cmdlist);
  2188.   add_cmd ("running", class_run, NO_FUNCTION, "Running the program.", &cmdlist);
  2189.  
  2190.   add_com ("pwd", class_files, pwd_command,
  2191.        "Print working directory.  This is used for your program as well.");
  2192.   add_com ("cd", class_files, cd_command,
  2193.        "Set working directory to DIR for debugger and program being debugged.\n\
  2194. The change does not take effect for the program being debugged\n\
  2195. until the next time it is started.");
  2196.  
  2197.   add_show_from_set
  2198.     (add_set_cmd ("prompt", class_support, var_string, (char *)&prompt,
  2199.        "Set gdb's prompt",
  2200.        &setlist),
  2201.      &showlist);
  2202.   
  2203.   add_com ("echo", class_support, echo_command,
  2204.        "Print a constant string.  Give string as argument.\n\
  2205. C escape sequences may be used in the argument.\n\
  2206. No newline is added at the end of the argument;\n\
  2207. use \"\\n\" if you want a newline to be printed.\n\
  2208. Since leading and trailing whitespace are ignored in command arguments,\n\
  2209. if you want to print some you must use \"\\\" before leading whitespace\n\
  2210. to be printed or after trailing whitespace.");
  2211.   add_com ("document", class_support, document_command,
  2212.        "Document a user-defined command.\n\
  2213. Give command name as argument.  Give documentation on following lines.\n\
  2214. End with a line of just \"end\".");
  2215.   add_com ("define", class_support, define_command,
  2216.        "Define a new command name.  Command name is argument.\n\
  2217. Definition appears on following lines, one command per line.\n\
  2218. End with a line of just \"end\".\n\
  2219. Use the \"document\" command to give documentation for the new command.\n\
  2220. Commands defined in this way do not take arguments.");
  2221.  
  2222. #ifdef __STDC__
  2223.   add_com ("source", class_support, source_command,
  2224.        "Read commands from a file named FILE.\n\
  2225. Note that the file \"" GDBINIT_FILENAME "\" is read automatically in this way\n\
  2226. when gdb is started.");
  2227. #else
  2228.   /* Punt file name, we can't help it easily.  */
  2229.   add_com ("source", class_support, source_command,
  2230.        "Read commands from a file named FILE.\n\
  2231. Note that the file \".gdbinit\" is read automatically in this way\n\
  2232. when gdb is started.");
  2233. #endif
  2234.  
  2235.   add_com ("quit", class_support, quit_command, "Exit gdb.");
  2236.   add_com ("help", class_support, help_command, "Print list of commands.");
  2237.   add_com_alias ("q", "quit", class_support, 1);
  2238.   add_com_alias ("h", "help", class_support, 1);
  2239.  
  2240.  
  2241.   c = add_set_cmd ("verbose", class_support, var_boolean, (char *)&info_verbose,
  2242.            "Set ",
  2243.            &setlist),
  2244.   add_show_from_set (c, &showlist);
  2245.   c->function.sfunc = set_verbose;
  2246.   set_verbose (NULL, 0, c);
  2247.   
  2248.   add_com ("dump-me", class_obscure, dump_me_command,
  2249.        "Get fatal error; make debugger dump its core.");
  2250.  
  2251.   add_show_from_set
  2252.     (add_set_cmd ("editing", class_support, var_boolean, (char *)&command_editing_p,
  2253.        "Set editing of command lines as they are typed.\n\
  2254. Use \"on\" to enable to enable the editing, and \"off\" to disable it.\n\
  2255. Without an argument, command line editing is enabled.  To edit, use\n\
  2256. EMACS-like or VI-like commands like control-P or ESC.", &setlist),
  2257.      &showlist);
  2258.  
  2259.   add_prefix_cmd ("history", class_support, set_history,
  2260.           "Generic command for setting command history parameters.",
  2261.           &sethistlist, "set history ", 0, &setlist);
  2262.   add_prefix_cmd ("history", class_support, show_history,
  2263.           "Generic command for showing command history parameters.",
  2264.           &showhistlist, "show history ", 0, &showlist);
  2265.  
  2266.   add_show_from_set
  2267.     (add_set_cmd ("expansion", no_class, var_boolean, (char *)&history_expansion_p,
  2268.        "Set history expansion on command input.\n\
  2269. Without an argument, history expansion is enabled.", &sethistlist),
  2270.      &showhistlist);
  2271.  
  2272.   add_show_from_set
  2273.     (add_set_cmd ("save", no_class, var_boolean, (char *)&write_history_p,
  2274.        "Set saving of the history record on exit.\n\
  2275. Use \"on\" to enable to enable the saving, and \"off\" to disable it.\n\
  2276. Without an argument, saving is enabled.", &sethistlist),
  2277.      &showhistlist);
  2278.  
  2279.   c = add_set_cmd ("size", no_class, var_uinteger, (char *)&history_size,
  2280.            "Set the size of the command history, \n\
  2281. ie. the number of previous commands to keep a record of.", &sethistlist);
  2282.   add_show_from_set (c, &showhistlist);
  2283.   c->function.sfunc = set_history_size_command;
  2284.  
  2285.   add_show_from_set
  2286.     (add_set_cmd ("filename", no_class, var_filename, (char *)&history_filename,
  2287.        "Set the filename in which to record the command history\n\
  2288.  (the list of previous commands of which a record is kept).", &sethistlist),
  2289.      &showhistlist);
  2290.  
  2291.   add_show_from_set
  2292.     (add_set_cmd ("confirm", class_support, var_boolean,
  2293.           (char *)&caution,
  2294.           "Set whether to confirm potentially dangerous operations.",
  2295.           &setlist),
  2296.      &showlist);
  2297.  
  2298.   add_prefix_cmd ("info", class_info, info_command,
  2299.         "Generic command for showing things about the program being debugged.",
  2300.           &infolist, "info ", 0, &cmdlist);
  2301.   add_com_alias ("i", "info", class_info, 1);
  2302.  
  2303.   add_prefix_cmd ("show", class_info, show_command,
  2304.           "Generic command for showing things about the debugger.",
  2305.           &showlist, "show ", 0, &cmdlist);
  2306.   /* Another way to get at the same thing.  */
  2307.   add_info ("set", show_command, "Show all GDB settings.");
  2308.  
  2309.   add_cmd ("commands", no_class, show_commands,
  2310.        "Show the the history of commands you typed.\n\
  2311. You can supply a command number to start with, or a `+' to start after\n\
  2312. the previous command number shown.",
  2313.        &showlist);
  2314.  
  2315.   add_cmd ("version", no_class, show_version,
  2316.        "Show what version of GDB this is.", &showlist);
  2317. }
  2318.